package com.agileboot.attendance.controller;

import com.agileboot.attendance.domain.rule.db.entity.AttendanceRule;
import com.agileboot.attendance.domain.rule.dto.AttendanceRuleDTO;
import com.agileboot.attendance.domain.rule.dto.AttendanceRuleQueryDTO;
import com.agileboot.attendance.domain.rule.service.IAttendanceRuleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 考勤规则Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/attendance/rule")
public class AttendanceRuleController {
    
    @Autowired
    private IAttendanceRuleService attendanceRuleService;
    
    /**
     * 分页查询考勤规则列表
     */
    @GetMapping("/list")
    public ResponseDTO<PageDTO<AttendanceRuleDTO>> list(AttendanceRuleQueryDTO queryDTO,
                                        @RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<AttendanceRule> page = new Page<>(pageNum, pageSize);
        IPage<AttendanceRuleDTO> pageData = attendanceRuleService.page(page, queryDTO);
        
        // 转换为统一的PageDTO格式
        PageDTO<AttendanceRuleDTO> result = new PageDTO<>();
        result.setRows(pageData.getRecords());
        result.setTotal(pageData.getTotal());
        result.setPageNum(pageData.getCurrent());
        result.setPageSize(pageData.getSize());
        
        return ResponseDTO.ok(result);
    }
    
    /**
     * 获取考勤规则详情
     */
    @GetMapping("/{ruleId}")
    public ResponseDTO<AttendanceRuleDTO> getInfo(@PathVariable Long ruleId) {
        AttendanceRuleDTO dto = attendanceRuleService.getInfo(ruleId);
        return ResponseDTO.ok(dto);
    }
    
    /**
     * 新增考勤规则
     */
    @PostMapping
    public ResponseDTO<Boolean> add(@RequestBody AttendanceRuleDTO ruleDTO) {
        boolean result = attendanceRuleService.add(ruleDTO);
        return ResponseDTO.ok(result);
    }
    
    /**
     * 修改考勤规则
     */
    @PutMapping
    public ResponseDTO<Boolean> update(@RequestBody AttendanceRuleDTO ruleDTO) {
        boolean result = attendanceRuleService.update(ruleDTO);
        return ResponseDTO.ok(result);
    }
    
    /**
     * 删除考勤规则
     */
    @DeleteMapping("/{ruleId}")
    public ResponseDTO<Boolean> delete(@PathVariable Long ruleId) {
        boolean result = attendanceRuleService.delete(ruleId);
        return ResponseDTO.ok(result);
    }
} 